package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.RvPeopleAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.BmPeopleInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 报名名单
 * Created by AdministHttpUtilsrator on 2019/4/2 0002.
 */

public class TeacherEventPeoplesActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;
    @BindView(R.id.ll_content)
    LinearLayout llConent;

    //空视图
    private View emptyView;

    //数据源
    private List<BmPeopleInfo.ListBean> oldDatas;

    //适配器
    private RvPeopleAdapter adapter;

    //请求的rowid
    private String rowId;

    private String userid;


    //请求的账号和参数
    private String acc, role;


    /**
     * 跳转到当前页面的方法
     */
    public static void goActivity(Context context, String rowid, String userid) {
        Intent it = new Intent(context, TeacherEventPeoplesActivity.class);
        it.putExtra("rowid", rowid);
        it.putExtra("userid", userid);
        context.startActivity(it);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_event_peoples;
    }

    @Override
    protected void init() {
        initStateBar();

        //获取传递参数
        initIntent();

        //初始化标题
        initTitle();

        //初始列表
        initRv();

        //初始化请求参数
        initParams();

        //请求数据
        http();
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("报名名单");
    }

    /**
     * 获取传递的intent对象
     */
    private void initIntent() {
        rowId = getIntent().getStringExtra("rowid");
        userid = getIntent().getStringExtra("userid");
    }


    /**
     * 初始化列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new RvPeopleAdapter(this, R.layout.rv_people_details, oldDatas);
        BqaManager.setRv(emptyView, this, adapter, rv);
    }

    /**
     * 初始化请求参数
     */
    private void initParams() {
        acc = AccountUtils.getAccount(this);
        role = AccountUtils.getRole(this);
    }


    /**
     * 请求页面数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "12");
        if ("3".equals(AccountUtils.getRole(this))) {
            map.put("acc", userid);
        } else {
            map.put("acc", acc);
        }

        map.put("role", "2");
        map.put("rowid", rowId);

        LogUtils.iTag("test", HttpAddress.EVENTPEOPLES + "?type=12&acc=" + acc + "&role=" + role + "&rowid=" + rowId);

        HttpUtils.post(HttpAddress.EVENTPEOPLES, map, this, new HttpCallBack<BmPeopleInfo>() {
            @Override
            public void onSuccess(BmPeopleInfo info) {
                rlLoading.setVisibility(View.GONE);
                llConent.setVisibility(View.VISIBLE);

                if ("200".equals(info.state)) {
                    List<BmPeopleInfo.ListBean> newDatas = info.list;
                    if (newDatas != null) {
                        oldDatas.addAll(newDatas);
                    }

                    BqaManager.setRv(emptyView, TeacherEventPeoplesActivity.this, adapter, rv);

                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + info.state);
                }


            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });

    }


    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }
}
